"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，扫描盘符/显示磁盘信息/读取dss&磁阵文件内容
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，扫描并打开指定路径下指定用户和属组的盘符,不走服务端
    2、进入交互模式，显示磁盘信息
    3、进入交互模式，显示磁盘信息，指定blockid
    4、进入交互模式，读取dss文件内容
    5、进入交互模式，读取此阵文件内容
Expect      :
    1、查看成功
    2、查看成功
    3、查看成功
    4、查看成功
    5、查看成功
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')
        self.vg_info_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.blockid_path = os.path.join(macro.DSS_INSTANCE_PATH, 'log/debug/dsscmd.dlog')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，扫描盘符信息   expect:查看成功-----'
        self.log.info(text)

        vg_info = f'cat {self.vg_info_path}'
        self.log.info(vg_info)
        vg_res = self.node.sh(vg_info).result()
        self.log.info(vg_res)
        vg_value = vg_res.splitlines()[0].split(':')[-1].strip()

        cmd = f'''dsscmd -i'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "scandisk -t block -p {vg_value} -u {self.node.ssh_user} -g {self.node.ssh_user}\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        res1 = self.node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Succeed to scan disk', res1, '扫描盘符失败')

        text = '-----step2:进入交互式模式，显示磁盘信息   expect:查看成功-----'
        self.log.info(text)

        param_list2 = ['core_ctrl', 'vg_header', 'volume_ctrl', 'root_ft_block']
        for param in param_list2:
            execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "showdisk -g data -s {param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd2)
            res2 = self.node.sh(execute_cmd2).result()
            self.log.info(res2)
            self.assertIn('Succeed to printf dss metadata', res2, '查看磁盘信息失败')

        for param in param_list2:
            execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "showdisk -g log -s {param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd3)
            res3 = self.node.sh(execute_cmd3).result()
            self.log.info(res3)
            self.assertIn('Succeed to printf dss metadata', res3, '查看磁盘信息失败')

        text = '-----step3:进入交互式模式，显示磁盘信息，指定block_id   expect:查看成功-----'
        self.log.info(text)

        blockid_info = f'cat {self.blockid_path} | grep blockid | tail -n 1'
        self.log.info(blockid_info)
        blockid_res = self.node.sh(blockid_info).result()
        self.log.info(blockid_res)
        block_id = blockid_res.split('metaid:')[-1].split('(')[0].strip()

        execute_cmd4 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "showdisk -g data -b {block_id} -n 0\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd4)
        res4 = self.node.sh(execute_cmd4).result()
        self.log.info(res4)
        self.assertIn(f'Succeed to printf dss file block with block_id:{block_id}',
                      res4, '查看磁盘信息失败')

        text = '-----step4:读取dss文件内容  expect:成功-----'
        self.log.info(text)

        log_name = vg_res.splitlines()[-1].split(':')[0].strip()
        xlog_info = f'source {macro.DB_ENV_PATH};dsscmd ls -p +{log_name}/pg_xlog0'
        self.log.info(xlog_info)
        xlog_res = self.node.sh(xlog_info).result()
        self.log.info(xlog_res)
        xlog_file = xlog_res.splitlines()[2].split()[5].strip()

        param_list4 = ['c', 'h', 'u', 'l', 's', 'x']
        for param in param_list4:
            execute_cmd5 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "examine -p +{log_name}/pg_xlog0/{xlog_file} -o 51200 -f {param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd5)
            res5 = self.node.sh(execute_cmd5).result()
            self.log.info(res5)
            self.assertIn('filename', res5, '读取dss文件内容失败')

        text = '-----step5:读取磁阵文件内容  expect:成功-----'
        self.log.info(text)

        for param in param_list4:
            execute_cmd6 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "dev -v {vg_value} -o 512 -f {param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd6)
            res6 = self.node.sh(execute_cmd6).result()
            self.log.info(res6)
            self.assertIn('volume path', res6, '读取磁阵文件内容失败')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
